// pages/login/login.js
// 安宇哲
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    activeIndex: 0,
    // 账号
    value1: '13111111111',
    // 密码
    value2: '12341234',
    // 验证码登录
    value3: '13111111111',
    value4: '1366',
    flag: true,
    flag1: false,
    // 用户
    data: [],
    token: '',
    math: null,
    // 注册
    sjhvalue: '1231231231',
    yzmvalue: '',
    mimavalue: '123',
    // 注册随机验证码
    math2: null,
    // 注册登录
    flagdl2: true
  },
  // 注册input值
  zhChange(e) {
    this.setData({
      sjhvalue: e.detail
    })

  },
  yzmChange(e) {
    this.setData({
      yzmvalue: e.detail
    })
  },
  mmChange(e) {
    this.setData({
      mimavalue: e.detail
    })

  },
  // 注册获取验证码
  getmsg() {
    this.setData({
      math2: Math.floor(Math.random() * (9999 - 1000)) + 1000
    })
    console.log(this.data.math2);
  },
  // 点击注册
  regs() {
    console.log(this.data.data);
    let that = this
    let arr = this.data.data.find((item) => {
      return item.username == this.data.sjhvalue
    }) || {}
    console.log(arr);
    if (arr.username == this.data.sjhvalue) {
      wx.showToast({
        title: '用户已存在',
        icon: 'error',
        duration: 2000
      })
      return false
    } else if (this.data.math2 == this.data.yzmvalue) {
      wx.request({
        url: 'http://localhost:7001/api/addLogin',
        method: 'post',
        data: {
          username: this.data.sjhvalue,
          password: this.data.mimavalue,
          msg: this.data.yzmvalue,
          userid: this.data.data.length + 1
        },
        success: function (res) {
          that.setData({
            activeIndex: 0
          })
          wx.showToast({
            title: '注册成功!',
            icon: 'success',
            duration: 2000
          })
        }
      })
      return false
    } else {
      wx.showToast({
        title: '验证码错误',
        icon: 'error',
        duration: 2000
      })
    }




  },
  // 验证码
  msg() {
    this.setData({
      math: Math.floor(Math.random() * (9999 - 1000)) + 1000
    })
    console.log(this.data.math);
  },
  // 通过密码登录
  tode2() {
    this.setData({
      flag1: false,
      flag: true,
      flagdl2: !this.data.flagdl2
    })
  },
  // 通过短信登录
  tode() {
    this.setData({
      flag1: true,
      flag: false,
      flagdl2: !this.data.flagdl2
    })
  },
  // 手机号
  onChange1(event) {
    let a = event.detail
    this.setData({
      value1: a
    })
  },
  // 密码
  onChange2(event) {
    let a = event.detail
    this.setData({
      value2: a
    })
  },
  onChange3(event) {
    let a = event.detail
    this.setData({
      value3: a
    })
  },
  onChange4(event) {
    let a = event.detail
    this.setData({
      value4: a
    })
  },
  // tab切换登录注册
  setTabIndex(e) {
    let activeIndex = e.currentTarget.dataset.i;
    this.setData({
      activeIndex
    })
  },
  // 密码登录
  login() {
    let that = this
    this.data.data.find((item) => {
      if (item.username == this.data.value1 && item.password == this.data.value2) {
        wx.request({
          url: 'http://localhost:7001/api/addMyUser',
          method: 'post',
          data: {
            username: this.data.value1,
            password: this.data.value2
          },
          success: function (res) {
            console.log(res);
            if (res.data.code == 200) {
              that.setData({
                token: res.data.data.token
              })
              wx.setStorageSync('name', that.data.value1)
              wx.setStorageSync('token', that.data.token)
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
              })
              wx.reLaunch({
                url: '/pages/home/home',
              })
            }
          }
        })
      }
    })
  },
  // 验证码登录
  login2() {
    let that = this
    this.data.data.find((item) => {
      if (item.username == this.data.value3 && item.msg == this.data.value4) {
        wx.request({
          url: 'http://localhost:7001/api/addMymsg',
          method: 'post',
          data: {
            username: this.data.value3,
            msg: this.data.value4
          },
          success: function (res) {
            if (res.data.code == 200) {
              console.log(33333);
              that.setData({
                token: res.data.data.token
              })
              wx.setStorageSync('name', that.data.value1)
              wx.setStorageSync('token', that.data.token)

              wx.reLaunch({
                url: '/pages/home/home',
              })
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
              })
            }
          }

        })
      } else {
        wx.showToast({
          title: '验证码错误',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  /*
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    wx.request({
      url: 'http://localhost:7001/api/getLogin',
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            data: res.data.data
          })
          console.log(that.data.data);
        }
      }
    })
    let token = wx.getStorageSync('token')
    if (token) {
      wx.showToast({
        title: '登录成功!',
        icon: 'success',
        duration: 2000
      })
    } else {
      wx.showToast({
        title: '请先登录!',
        icon: 'error',
        duration: 2000
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})